const Service = require('egg').Service;
const ResultModel = require('../model/resultModel');

class GoldService extends Service {
    async findUser(account){
        let user = await this.app.mysql.get('cp_user',{account:account});
        return user;
    }

    async insertUser(name,phone){
        let timestamp = new Date().getTime();
        timestamp = parseInt(timestamp/1000+'');
        const row = {
            nickname:name,
            account:-1,
            role:1,
            password:'e10adc3949ba59abbe56e057f20f883e',
            phone:phone,
            head:'//img.codeplanet.cc/img/face/71922fb223412a80aafcacfe0ca0f28b.jpg',
            created:timestamp,
            remark:'2554'
        }

        const result = await this.app.mysql.insert('cp_user',row);
        console.log('insertUser '+JSON.stringify(result));
        const account = result.insertId + 10000;
        let updateRow;
        if(name==null || name == ''){
            updateRow = {
                nickname:account,
                account:account
            }
        }else{
            updateRow = {
                account:account
            }
        }
        

        const options = {
            where: {
                uid:result.insertId
            }
          };
        const ret = await this.app.mysql.update('cp_user',updateRow,options);
        console.log('insertUser update account '+JSON.stringify(ret));
        return ret;
    }

    async findUserLive(uid){
        let user = await this.app.mysql.get('cp_user_live',{uid:uid});
        return user;
    }

    async insertUserLive(uid,golds){
        let timestamp = new Date().getTime();
        timestamp = parseInt(timestamp/1000+'');
        const row = {
            uid:uid,
            gold:golds,
            created:timestamp
        }

        const result = await this.app.mysql.insert('cp_user_live',row);
        return result;
    }

    async updateUserLive(uid,golds){
        const row = {
            gold:golds
        }

        const options = {
            where: {
                uid:uid
            }
          };

        const result = await this.app.mysql.update('cp_user_live',row,options);
        return result;
    }

    async batchCreatedUser(userDatas){
        // let sucessData = new Array<number>();
        // let failData = new Array<number>();
        for (const userdata of userDatas) {
            let ret = await this.insertUser(userdata.name,userdata.phone);
            if(ret.affectedRows !== 1){
                
                
            }
        }

        return ResultModel.createResult(200);
    }

    async batchUpdateGold(accounts,gold){
        let sucessData = new Array();
        let failData = new Array();
        for (let account of accounts) {
            let ret = await this.updateGold(account,gold);
            if(ret.statue == 200){
                sucessData.push(account);
            }else{
                failData.push(account);
            }
        }

        return ResultModel.createResult(200,{"sucess":sucessData,"fail":failData});
    }

    async updateGold(account,gold){
        let ret;

        //找到用户
        console.log('account '+account+' gold '+gold);
        const user = await this.findUser(account);
        if(!user){
            return ResultModel.createResult(401,'没有该用户');
        }
        console.log('user '+JSON.stringify(user));

        //找到用户的gold表
        const goldData = await this.findUserLive(user.uid);
        console.log('userLive '+JSON.stringify(goldData));

        
        //有gold表就更新数据
        if(goldData){
            ret = await this.updateUserLive(user.uid,goldData.gold+gold);
            if(ret.affectedRows !== 1){
                return ResultModel.createResult(402,"更新金币失败");
            }
        }else{
            //没有gold表就插入数据
            ret = await this.insertUserLive(user.uid,gold);
            if(ret.affectedRows !== 1){
                return ResultModel.createResult(403,"更新金币失败");
            }
        }

        return ResultModel.createResult(200);
    }
}

module.exports = GoldService;